home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cc02.zip / CPFILE.C < prev    next >
Text File  |  1984-08-19  |  931b  |  61 lines

  1. /* -- cpfile.c    copies from first file to second file <byte by byte> -- */
  2.  
  3. #include "stdio.h"
  4.  
  5. main(argc, argv)        /* copy file a byte at a time */
  6.  
  7.     int argc;
  8.     char *argv[];
  9.  
  10.     {
  11.         int c;
  12.         FILE *infile, *outfile;
  13.  
  14.         if (argc < 3)
  15.             errexit("Usage: cpfile oldfile newfile", NULL);
  16.         if (strcmp(argv[1], argv[2]) == 0)
  17.             errexit("File names must be different", NULL);
  18.         if ((infile = fopen(argv[1], "r")) == NULL)
  19.             errexit("Can't open", argv[1]);
  20.         if ((outfile = fopen(argv[2], "w")) == NULL)
  21.             errexit("Can't create", argv[2]);
  22.  
  23.         printf("File %s ", argv[1]);
  24.  
  25.         while ((c = getc(infile)) != EOF)
  26.             putc(c, outfile);
  27.  
  28.         fclose(infile);
  29.         fclose(outfile);
  30.  
  31.         printf("copied to %s\n", argv[2]);
  32.         exit(0);
  33.     }
  34.  
  35. errexit(s1, s2)         /* print error message and die */
  36.  
  37.     char *s1, *s2;
  38.  
  39.     {
  40.         printf(s2 == NULL ? "%s\n" : "%s %s\n", s1, s2);
  41.         exit(-1);
  42.     }
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.